package com._520it.crm.service.impl;

import com._520it.crm.domain.BankName;
import com._520it.crm.domain.Organization;
import com._520it.crm.domain.Pos;
import com._520it.crm.mapper.BankNameMapper;
import com._520it.crm.mapper.OrganizationMapper;
import com._520it.crm.mapper.PosMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.PosQueryObject;
import com._520it.crm.service.IPosService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class PosServiceImpl implements IPosService {
    
    @Autowired
    private PosMapper posMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private BankNameMapper bankNameMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return posMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Pos record) {
        return posMapper.insert(record);
    }

    @Override
    public Pos selectByPrimaryKey(Long id) {
        return posMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Pos> selectAll() {
        return posMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Pos record) {
        return posMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryPage(PosQueryObject qo) {
        if(StringUtils.isNotBlank(qo.getOrganizationNameP())){
            Organization organization = organizationMapper.selectByPrimaryKey(Long.parseLong(qo.getOrganizationNameP()));
            qo.setOrganizationNameP(organization.getOrganizationName());
        }
        if(StringUtils.isNotBlank(qo.getBankNameSnP())){
            BankName bankName = bankNameMapper.selectByPrimaryKey(Long.parseLong(qo.getBankNameSnP()));
            qo.setBankNameSnP(bankName.getSn());
        }
        Long count = posMapper.queryPageCount(qo);
        if (count <= 0) {
            return new PageResult(0L, Collections.EMPTY_LIST);
        }
        List result = posMapper.queryPageData(qo);
        return new PageResult(count,result);
    }
}
